package com.tianqu.tms.www.cardect.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.entity.TmsDetectPic;
import com.tianqu.tms.entity.TmsDetectSenior;
import com.tianqu.tms.www.cardect.viewhelper.CarSeniorDetectViewHelper;

/**
 * 高级检测报告录入Controller
 * 
 * @author tianshifu
 * 
 */
public class CarWDPersonSeniorDetectTopResultController extends TmsController {

	private Logger logger = Logger
			.getLogger(CarWDPersonSeniorDetectTopResultController.class);// log
	private static final String SAVE_STATUS = "save_status";// 保存状态
	private static final String SENIOR_DETECT_INPUT_DATA = "senior_detect_input_data";// 检测录入信息
	private static final String CARSERIAL = "carSerial";// 检测录入信息
	private static final String IMGLIST = "imgList";// 用于存放图片对象的list

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			// 从viewhelp中取出检测信息
			// 从uc中取出车辆ID和保存状态（暂时保存和提交保存）
			String carSerial = (String) uc.getAttribute(CARSERIAL);
			uc.removeAttribute(CARSERIAL);

			String status = (String) uc.getAttribute(SAVE_STATUS);// 获取状态
																	// 存在tis_car_info表中
			uc.removeAttribute(SAVE_STATUS);
			CarSeniorDetectViewHelper view = (CarSeniorDetectViewHelper) uc
					.getAttribute(SENIOR_DETECT_INPUT_DATA);// 检测信息
			// 先在车辆基本信息表中查检测ID是否存在，存在则更新，不存在则添加
			// 根据车辆ID和将检测ID和车辆状态 八大配置 行驶里程 检测模式（基本）检测时间 更新者ID 事故车 车况描述
			TmsDetectInfo carInfo = TmsDetectInfo.getInstance();
			// 需要根据车辆ID查询出登记日期
			carInfo = carInfo.getWDIpadCarInfoBySerial(carSerial, 1);
			setCarInfoFromSeniorViewhelp(carInfo, view);//

			// 先存检测信息 返回一个检测ID 在tis_car_info表更新和tis_detect_pic表中进行更新和新增
			TmsDetectSenior entity = TmsDetectSenior.getInstance();

			TmsDetectPic picEntity = TmsDetectPic.getInstance();
			entity = setEntityFromView(entity, view);

			if (carInfo.getCarDetectId() > 0) {
				// 更新
				entity.setId(carInfo.getCarDetectId());
				entity.updateCarSeniorInfo(entity);

				picEntity.delPic(carInfo.getCarDetectId());
				// 传一个list和一个检测ID，检测模式1
				List<TmsDetectPic> list = (List<TmsDetectPic>) uc
						.getAttribute(IMGLIST);

				String sysPath = (String) uc.getAttribute("sysPath");

				picEntity.insertTisPicInfo(list, sysPath, carInfo
						.getCarDetectId(), carInfo.getCarSerial(), 2);
				uc.removeAttribute(IMGLIST);
			} else {
				// 添加
				// 执行高级检测报告存入功能
				int carDetectId = entity.insertCarSeniorDetectInfo(entity);
				carInfo.setCarDetectId(carDetectId);// 检测ID

				// 添加图片信息
				// 传一个list和一个检测ID，检测模式1
				List<TmsDetectPic> list = (List<TmsDetectPic>) uc
						.getAttribute(IMGLIST);

				String sysPath = (String) uc.getAttribute("sysPath");

				picEntity.insertTisPicInfo(list, sysPath, carDetectId, carInfo
						.getCarSerial(), 2);
				uc.removeAttribute(IMGLIST);
			}
			TmsUserProfile profile = uc.getUserProfile();

			carInfo.setCarSerial(carSerial);// 车辆ID
			carInfo.setStatus(Integer.parseInt(status));// 车辆状态
			carInfo.setUpdateUser(profile.getUniqueId());

			// 更新基本信息表
			carInfo.updateCarInfoByNormalDetectInfo(carInfo, 2);

			// 这块需要更新下竞价基本信息表 参数：车辆ID 表显里程
			AucCarInfo auc = AucCarInfo.getInstance();

			auc.updateMeliageBySerial(carSerial, Integer.parseInt(view
					.getMileage()));

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}

	/**
	 * 
	 * 将viewhelp中的信息set到entity中用于传递保存
	 * 
	 * @throws TmsDaoException
	 * 
	 */
	private TmsDetectSenior setEntityFromView(TmsDetectSenior entity,
			CarSeniorDetectViewHelper view) throws TmsDaoException {
		// 检测信息
		/******** 事故项检查 ************/
		entity.setBodyChk1(StringUtil.subStrTool(view.getBodyChk1()));
		entity.setBodyChk2(StringUtil.subStrTool(view.getBodyChk2()));
		entity.setBodyChk3(StringUtil.subStrTool(view.getBodyChk3()));
		entity.setBodyChk4(StringUtil.subStrTool(view.getBodyChk4()));
		entity.setBodyChk5(StringUtil.subStrTool(view.getBodyChk5()));
		entity.setBodyChk6(StringUtil.subStrTool(view.getBodyChk6()));
		entity.setBodyChk7(StringUtil.subStrTool(view.getBodyChk7()));
		entity.setBodyChk8(StringUtil.subStrTool(view.getBodyChk8()));
		entity.setBodyChk9(StringUtil.subStrTool(view.getBodyChk9()));
		entity.setBodyChk10(StringUtil.subStrTool(view.getBodyChk10()));
		entity.setBodyChk11(StringUtil.subStrTool(view.getBodyChk11()));
		entity.setBodyChk12(StringUtil.subStrTool(view.getBodyChk12()));
		entity.setBodyChk13(StringUtil.subStrTool(view.getBodyChk13()));

		/*********** 技术检测，车身外观检测 ***************/
		entity.setOutwardChk1(StringUtil.subStrTool(view.getOutwardChk1()));
		entity.setOutwardChk2(StringUtil.subStrTool(view.getOutwardChk2()));
		entity.setOutwardChk3(StringUtil.subStrTool(view.getOutwardChk3()));
		entity.setOutwardChk4(StringUtil.subStrTool(view.getOutwardChk4()));
		entity.setOutwardChk5(StringUtil.subStrTool(view.getOutwardChk5()));
		entity.setOutwardChk6(StringUtil.subStrTool(view.getOutwardChk6()));
		entity.setOutwardChk7(StringUtil.subStrTool(view.getOutwardChk7()));
		entity.setOutwardChk8(StringUtil.subStrTool(view.getOutwardChk8()));
		entity.setOutwardChk9(StringUtil.subStrTool(view.getOutwardChk9()));
		entity.setOutwardChk10(StringUtil.subStrTool(view.getOutwardChk10()));
		entity.setOutwardChk11(StringUtil.subStrTool(view.getOutwardChk11()));
		entity.setOutwardChk12(StringUtil.subStrTool(view.getOutwardChk12()));
		entity.setOutwardChk13(StringUtil.subStrTool(view.getOutwardChk13()));
		entity.setOutwardChk14(StringUtil.subStrTool(view.getOutwardChk14()));
		entity.setOutwardChk15(StringUtil.subStrTool(view.getOutwardChk15()));
		entity.setOutwardChk16(StringUtil.subStrTool(view.getOutwardChk16()));
		entity.setOutwardChk17(StringUtil.subStrTool(view.getOutwardChk17()));
		entity.setOutwardChk18(StringUtil.subStrTool(view.getOutwardChk18()));
		entity.setOutwardChk19(StringUtil.subStrTool(view.getOutwardChk19()));
		entity.setOutwardChk20(StringUtil.subStrTool(view.getOutwardChk20()));
		entity.setOutwardChk21(StringUtil.subStrTool(view.getOutwardChk21()));
		entity.setOutwardChk22(StringUtil.subStrTool(view.getOutwardChk22()));
		entity.setOutwardChk23(StringUtil.subStrTool(view.getOutwardChk23()));
		entity.setOutwardChk25(StringUtil.subStrTool(view.getOutwardChk24()));
		entity.setOutwardChk26(StringUtil.subStrTool(view.getOutwardChk25()));
		entity.setOutwardChk27(StringUtil.subStrTool(view.getOutwardChk26()));

		/*********** 技术检测，发动机舱检测 ***************/
		// 从字典表中通过value值找到对应的code进行保存 没有则给个0
		entity.setEngineChk1(strTransToInt(view.getEngineChk1()));
		entity.setEngineChk2(strTransToInt(view.getEngineChk2()));
		entity.setEngineChk3(strTransToInt(view.getEngineChk3()));
		entity.setEngineChk4(strTransToInt(view.getEngineChk4()));
		entity.setEngineChk5(strTransToInt(view.getEngineChk5()));
		entity.setEngineChk6(strTransToInt(view.getEngineChk6()));
		entity.setEngineChk7(strTransToInt(view.getEngineChk7()));
		entity.setEngineChk8(strTransToInt(view.getEngineChk8()));
		entity.setEngineChk9(strTransToInt(view.getEngineChk9()));

		/*********** 技术检测，驾驶舱检测 ***************/
		entity.setPilotChk1(strTransToInt2(view.getPilotChk1()));
		entity.setPilotChk2(strTransToInt2(view.getPilotChk2()));
		entity.setPilotChk3(strTransToInt2(view.getPilotChk3()));
		entity.setPilotChk4(strTransToInt2(view.getPilotChk4()));
		entity.setPilotChk5(strTransToInt2(view.getPilotChk5()));
		entity.setPilotChk6(strTransToInt2(view.getPilotChk6()));
		entity.setPilotChk7(strTransToInt2(view.getPilotChk7()));
		entity.setPilotChk8(strTransToInt2(view.getPilotChk8()));
		entity.setPilotChk9(strTransToInt2(view.getPilotChk9()));
		entity.setPilotChk10(strTransToInt2(view.getPilotChk10()));
		entity.setPilotChk11(strTransToInt2(view.getPilotChk11()));
		entity.setPilotChk12(strTransToInt2(view.getPilotChk12()));
		entity.setPilotChk13(strTransToInt2(view.getPilotChk13()));
		entity.setPilotChk14(strTransToInt2(view.getPilotChk14()));

		/*********** 技术检测，启动检测 ***************/
		entity.setStartChk1(strTransToInt2(view.getStartChk1()));
		entity.setStartChk2(strTransToInt2(view.getStartChk2()));
		entity.setStartChk3(strTransToInt2(view.getStartChk3()));
		entity.setStartChk4(strTransToInt2(view.getStartChk4()));
		entity.setStartChk5(strTransToInt2(view.getStartChk5()));
		entity.setStartChk6(strTransToInt2(view.getStartChk6()));
		entity.setStartChk7(strTransToInt2(view.getStartChk7()));
		entity.setStartChk8(strTransToInt2(view.getStartChk8()));
		entity.setStartChk9(strTransToInt2(view.getStartChk9()));
		/*********** 技术检测，路试检测 ***************/
		entity.setRunChk1(strTransToInt2(view.getRunChk1()));
		entity.setRunChk2(strTransToInt2(view.getRunChk2()));
		entity.setRunChk3(strTransToInt2(view.getRunChk3()));
		entity.setRunChk4(strTransToInt2(view.getRunChk4()));
		entity.setRunChk5(strTransToInt2(view.getRunChk5()));
		entity.setRunChk6(strTransToInt2(view.getRunChk6()));
		entity.setRunChk7(strTransToInt2(view.getRunChk7()));
		entity.setRunChk8(strTransToInt2(view.getRunChk8()));
		entity.setRunChk9(strTransToInt2(view.getRunChk9()));
		/*********** 技术检测，底盘检测 ***************/
		entity.setBottomChk1(strTransToInt2(view.getBottomChk1()));
		entity.setBottomChk2(strTransToInt2(view.getBottomChk2()));
		entity.setBottomChk3(strTransToInt2(view.getBottomChk3()));
		entity.setBottomChk4(strTransToInt2(view.getBottomChk4()));
		entity.setBottomChk5(strTransToInt2(view.getBottomChk5()));
		entity.setBottomChk6(strTransToInt2(view.getBottomChk6()));
		entity.setBottomChk7(strTransToInt2(view.getBottomChk7()));

		/********** 评分 *****************/
		entity.setOutwardScore(view.getOutwardScore());
		entity.setEngineScore(view.getEngineScore());
		entity.setPilotScore(view.getPilotScore());
		entity.setStartScore(view.getStartScore());
		entity.setRunScore(view.getRunScore());
		entity.setBottomScore(view.getBottomScore());

		return entity;
	}

	/**
	 * 
	 * 车辆基本信息表更新
	 * 
	 * @throws TmsDaoException
	 */
	private TmsDetectInfo setCarInfoFromSeniorViewhelp(TmsDetectInfo carInfo,
			CarSeniorDetectViewHelper view) throws TmsDaoException {

		carInfo.setTroubleCar(view.getTroubleCar());// 事故车 1是u 2不是
		carInfo.setCarDescribe(view.getCarDescribe());// 车况描述
		carInfo.setMileage(Integer.parseInt(view.getMileage()));// 行驶里程

		/********** 评分 *****************/
		double outwardScore = view.getOutwardScore();// 车身评分
		double engineScore = view.getEngineScore();// 发动机舱评分
		double pilotScore = view.getPilotScore();// 驾驶舱评分
		double startScore = view.getStartScore();// 启动评分
		double runScore = view.getRunScore();// 路试评分
		double bottomScore = view.getBottomScore();// 底盘评分

		double zf = outwardScore + engineScore + pilotScore + startScore
				+ runScore + bottomScore;// 总评分
		String zfPj = "";

		if (zf >= 90) {
			zfPj = "一级";
		}
		if (zf < 90 && zf >= 60) {
			zfPj = "二级";
		}
		if (zf < 70 && zf >= 20) {
			zfPj = "三级";
		}
		if (zf < 20) {
			zfPj = "四级";
		}
		if (view.getTroubleCar() == 1) {
			// 事故车
			carInfo.setTotalScore("五级");
		} else {

			carInfo.setTotalScore(zfPj);
		}
		return carInfo;
	}

	/**
	 * value和code转化
	 * 
	 * 无 轻微 严重 这块需要对“无”进行处理一下
	 */
	private int strTransToInt(String str) {
		if (str == null || "".equals(str)) {
			return 0;
		} else if (str.equals("无")) {
			return 0;
		} else if (str.equals("轻微")) {
			return 1;
		} else if (str.equals("严重")) {
			return 2;
		}

		return 0;
	}

	/**
	 * 将“是” 和 “否”转换成1 和 2
	 * 
	 * @param str
	 * @return
	 */
	private int strTransToInt2(String str) {
		if (str.equals("是")) {
			return 1;
		} else if (str.equals("否")) {
			return 2;
		}
		return 0;
	}
}
